package com.fenghongzhang.day009;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

public class Main2Activity extends AppCompatActivity {

    private Spinner sp1;
    private Spinner sp2;
    private Spinner sp3;

    private int index = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        //1 初始化
        sp1 = (Spinner) findViewById(R.id.sp1);
        sp2 = (Spinner) findViewById(R.id.sp2);
        sp3 = (Spinner) findViewById(R.id.sp3);
        //2 数据源

        final String[] sheng = {"河北省","河南省","山东省"};
        final String[][] shi = new String[][]{{"石家庄","保定","邯郸"},{"洛阳","郑州","开封"},{"烟台","青岛","菏泽"}};
        //  {{{}},{{}},{{}}}
        final String[][][] qu = new String[][][]{{{"河东","河西"},{"熊县","安县"},{"铁东","铁西"}},{{"河东1","河西1"},{"熊县1","安县1"},{"铁东1","铁西1"}}};
        //3 适配器
        ArrayAdapter<String> shengAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sheng);
        //4,设置适配器
        sp1.setAdapter(shengAdapter);
        //设置监听器
        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                index = position;
                //给第2个sp设置数据源
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(Main2Activity.this, android.R.layout.simple_list_item_1,shi[position]);
                sp2.setAdapter(stringArrayAdapter);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //给第2个sp设置数据源
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(Main2Activity.this, android.R.layout.simple_list_item_1, qu[index][position]);
                sp3.setAdapter(stringArrayAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
